package com.rocky.pubimport.service.strategy;

import com.rocky.pubimport.service.IPubImportRecordProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * 公共数据导入处理策略类
 * @author rocky
 * @date 2022/4/13 16:53
 */
@Component
public class PubImportRecordProcessServiceStrategyHolder {

    /** 存放对应的服务类 */
    private final Map<String, IPubImportRecordProcessService> INIT_MAP = new HashMap<>();

    /** Spring容器 */
    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init(){
        // 根据接口类型从Spring容器里面加载相应的所有bean，避免每新增一个类就要手动put进去Map
        Map<String, IPubImportRecordProcessService> map = applicationContext.getBeansOfType(IPubImportRecordProcessService.class);
        for (IPubImportRecordProcessService service : map.values()) {
            INIT_MAP.put(service.getModel(), service);
        }
    }
    /**
     * 根据业务模块获取对应的策略处理类
     * @param model 业务模块
     * @return IPubImportRecordProcessService 对应的策略处理类
     */
    public IPubImportRecordProcessService getStrategyService(String model) {
        return INIT_MAP.get(model);
    }



}
